﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using XWord.Fundamental.Domain;
using NHibernateUnitOfWork;
namespace ConsoleApplication1
{
    public class NetWord
    {
        public static Dictionary<string, object> aa(string word,XWord.Fundamental.Domain.Word entity)
        {
            var dic = new Dictionary<string, object>();
            string a = string.Empty;
            dic["def"] = string.Empty;
            dic["audio"] = string.Empty;
            var list = new List<Sentence>();
            dic["sentence"] = list;
            WebRequest request = WebRequest.Create(string.Format("http://dict.cn/ws.php?utf8=true&q={0}",word));
            
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream,Encoding.UTF8);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            if (!responseFromServer.Contains("Not Found"))
            {
                XmlDocument doc = new XmlDocument();
                doc.InnerXml = responseFromServer;
               // doc.g
                // Display the content.
                var node = doc.DocumentElement.SelectNodes("descendant::def");
                if (node.Count > 0)
                {
                    dic["def"] = node[0].InnerText;
                    a = node[0].InnerText;
                    Console.WriteLine(word+"  " +a);
                }
                var audionode = doc.DocumentElement.SelectNodes("descendant::audio");
                if (audionode.Count > 0)
                {
                    dic["audio"] = audionode[0].InnerText;
                   
                }
                var sentnode = doc.DocumentElement.SelectNodes("descendant::sent");
                
                if (sentnode.Count > 0)
                {
                   //System.Xml.x

                    if (entity.Sentences.Count == 0)
                    {
                        foreach (var item in sentnode)
                        {
                            XmlNode current = item as XmlNode;
                            Sentence sentence = new Sentence();

                            sentence.Origin = current.SelectNodes("orig")[0].InnerText;
                            sentence.Translate = current.SelectNodes("trans")[0].InnerText;
                            UnitOfWork.CurrentSession.Persist(sentence);
                            list.Add(sentence);

                            // item as XmlElement
                        }
                    }
                    
                }
            }
            else
            {
                Console.WriteLine(word + "/Not Found");
            }
            
            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
            return dic;
           
        }
    }
}
